package org.jeecg.common.util;


import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.entity.TemplateExportParams;
import org.jeecgframework.poi.excel.export.styler.ExcelExportStylerDefaultImpl;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: ExcelUtiles
 * @Description: Excel导入导出工具类
 * @author lidm
 * @date 2020-04-16 13:00:00
 *
 */
public class ExcelUtiles {

	public static <T,R extends ExcelExportStylerDefaultImpl> void ExportExcel(
			String fileName,
			String sheetName,
			List<T> dataSet,
			Class<T> clazz,
			Class<R> ExcelExportStylerClass,
			HttpServletResponse response) throws Exception {

		ExportParams params = new ExportParams();
		params.setStyle(ExcelExportStylerClass);
		params.setFixedTitle(false);
		params.setColor(HSSFColor.HSSFColorPredefined.WHITE.getIndex());
		Workbook workbook = ExcelExportUtil.exportExcel(params, clazz, dataSet);
		workbook.setSheetName(0, sheetName);
		setResponseHeader(response,fileName);
		workbook.write(response.getOutputStream());
	}

	public static <T> void ExportExcelNoTemplate(
			String fileName,
			String sheetName,
			List<T> dataSet ,
			Class<T> clazz,
			HttpServletResponse response) throws Exception{

		Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(),clazz,dataSet);
		workbook.setSheetName(0, sheetName);
		setResponseHeader(response,fileName);
		workbook.write(response.getOutputStream());
	}

	/**
	 * 根据模板导出excel
	 * 
	 * @param templetePath 模板路径 模板设计请参考相关文档
	 * @param sheetName    导出工作表名称
	 * @param fileName     导出excel文件名
	 * @param summaries    摘要数据
	 * @param rows         明细数据
	 * @param response     response对象
	 * @throws Exception
	 */
	public static void exportExcel(String templetePath, String sheetName, String fileName,
			Map<String, Object> summaries, List<?> rows, HttpServletResponse response) throws Exception {
		TemplateExportParams params = new TemplateExportParams(templetePath);

		if (summaries == null)
			summaries = new HashMap<String, Object>();

		if (rows != null && rows.size() > 0)
			summaries.put("rows", rows);

		Workbook workbook = ExcelExportUtil.exportExcel(params, summaries);
		workbook.setSheetName(0, sheetName);

		setResponseHeader(response,fileName);
		workbook.write(response.getOutputStream());

	}


	private static void setResponseHeader(HttpServletResponse response, String fileName) throws Exception {
		response.setCharacterEncoding("UTF-8");
		response.setHeader("content-Type", "application/vnd.ms-excel");
		response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
	}
	/**
	 * 根据模板导出excel 默认文件名为yyyyMMddHHmmssSSS.xls，默认工作表名为Sheet1
	 * 
	 * @param templetePath 模板路径 模板设计请参考相关文档
	 * @param summaries    摘要数据
	 * @param rows         明细数据
	 * @param response     response对象
	 * @throws Exception
	 */
	public static void exportExcel(String templetePath, Map<String, Object> summaries, List<?> rows,
			HttpServletResponse response) throws Exception {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");

		exportExcel(templetePath, "Sheet1", sdf.format(new Date()) + ".xls", summaries, rows, response);

	}

	/**
	 * 导入excel并转换为DTO列表
	 * 
	 * @param <T>         DTO类单独建立  必须使用EasyPOI注解
	 * @param file        上传的文件
	 * @param titleRows   标题行数 必须大于0否则返回null
	 * @param headerRows  表头行数 必须大于0
	 * @param dtoClass DTO类
	 * @return DTO列表
	 * @throws Exception
	 */
	public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows,
			Class<T> dtoClass) throws Exception {
		if (file == null || file.getSize() == 0)
			throw new Exception("没有文件");
		if (titleRows == null || titleRows < 0)
			throw new Exception("titleRows参数错误");
		if (headerRows == null || headerRows < 0)
			throw new Exception("headerRows参数错误");

		ImportParams params = new ImportParams();
		params.setTitleRows(titleRows);
		params.setHeadRows(headerRows);
		List<T> list = null;

		list = ExcelImportUtil.importExcel(file.getInputStream(), dtoClass, params);

		return list;
	}

}
